package cz.acies.genea.ui.svg;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonObject;

public class G2Polygon extends G2Object
{
	private static final long serialVersionUID = -6888857589942719107L;

	public static final int POLYGON = 0;
	public static final int POLYLINE = 1;
	
	private List<G2Point> points;
	 
	public G2Polygon() {
		points = new ArrayList<G2Point>();
	}
	
	public G2Polygon(G2Point... p) {
		this();
		for (int i = 0; i < p.length; i++) {
			this.addPoint(p[i]);
		}
	}

	public G2Polygon(List<G2Point> list) {
		this();
		for (int i = 0; i < list.size(); i++) {
			this.addPoint(list.get(i));
		}
	}

	// -----  -----------------------------------------------------------------

	@Override
	public JsonObject toJSON(JsonObject element) {
		return element;
	}

	// -----  -----------------------------------------------------------------
	
	@Override
	public String getType() {
		return "path";
	}

	// -----  -----------------------------------------------------------------
	
	public List<G2Point> getPoints() {
		return points;
	}

	/**
	 * Nahradi stavajici seznam bodu za novy
	 * @param points
	 */
	public void setPoints(List<G2Point> points) {
		this.points = points;
	}
	
	/**
	 * Prida na konec seznamu bodu, novy bod  
	 * @param p
	 */
	public void addPoint(G2Point p) {
		this.points.add(p);
	}

	public void setPoint(int index, G2Point p) {
		if (index < this.points.size()) {			
			this.points.set(index, p);
		} else {
			addPoint(p);
		}
	}

	// -----  -----------------------------------------------------------------

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "G2Polygon "+points;
	}
	
	/**
	 * Prevede seznam pointu do pole 
	 * @return
	 */
	public int[] toArray() {
		int sz = points.size();
		int[] ad = new int[points.size() * 2];
		for (int i = 0; i < sz; i++) {			
			ad[(i*2)] = (int) Math.floor(points.get(i).x);
			ad[(i*2)+1] = (int) Math.floor(points.get(i).y);
		}
		return ad;
	}
	
}
